package com.zyd.blog.personal.service.impl;

import com.zyd.blog.persistence.mapper.UserNoMapper;
import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.service.UserNoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户基本信息业务逻辑层
 * @author mfy
 */
@Service
public class UserNoServiceImpl implements UserNoService {

    @Resource
    private UserNoMapper userNoMapper;

    @Override
    public Boolean isHaveUserNo(Long userId){
        if(userId == null){
            return false;
        }
        Map<String,Object> userNo = getUserNoByUserId(userId);
        return !(userNo == null || userNo.size() == 0);
    }

    @Override
    public Map<String,Object> getUserNoByUserId(Long userId){
        if(userId == null){
            return new HashMap<>(0);
        }
        List<Map<String,Object>> userNos = getUserNoByCondition(userId, IsDelete.IS_DELETE_NO, Status.STATUS_YES);
        if(userNos == null || userNos.size() == 0){
            return new HashMap<>(0);
        }
        return userNos.get(0);
    }

    /**
     * 查询用户的情况
     * @param userId    用户id
     * @param isDelete  是否删除
     * @param status    存在状态
     * @return  返回用户信息
     */
    @Override
    public List<Map<String,Object>> getUserNoByCondition(Long userId,String isDelete,String status){
        if(userId == null){
            return new ArrayList<>();
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("isDelete", isDelete);
        condition.put("status", status);
        condition.put("id", userId);
        return userNoMapper.queryUserNoByCondition(condition);
    }


    @Override
    public Boolean updateUserNo(Map<String,Object> userNoCondition){
        Long id = (Long) userNoCondition.get("id");
        if(id == null){
            return false;
        }
        userNoCondition.put("updateTime",(new Date()).getTime());
        return userNoMapper.updateUserNo(userNoCondition) > 0;
    }

    @Override
    public int saveUserNo(Map<String,Object> userNoCondition) {
        return userNoMapper.addUserNo(userNoCondition);
    }

}
